const { Model, DataTypes } = require('sequelize')

const sequelize  = require('../utils/db')
const md5 = require('../utils/md5')

class User extends Model {}

User.init({
    username: {
        type: DataTypes.STRING,
        allowNull: false
    },
    email: {
        type: DataTypes.STRING,
        allowNull: false
    },
    bio: {
        type: DataTypes.STRING,
        defaultValue: ''
    },
    avatar: {
        type: DataTypes.STRING,
        defaultValue: ''
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false,
        set(value) {
            this.setDataValue('password', md5(value))
        }
    }
}, {
    sequelize,
    tableName: 'user'
})

module.exports = {
    User
}
